package xing.infrastructure.data;

import org.springframework.stereotype.Component;

/**
 * Created by dustin on 2017/9/3.
 */
@Component
public class ResultHolder {
    private static ThreadLocal<Object> model = new ThreadLocal<>();

    public void clean(){
        model.remove();
    }

    public CallResult getModel(){
        Object o = model.get();
//        if(o == null){
//            this.setModel(CallResult.ok());
//            o = this.getObject();
//        }
        if(o != null && o instanceof CallResult){
            return (CallResult) o;
        }else {
            return null;
        }
    }

    public Object getObject(){
        return model.get();
    }

    public void setModel(Object o){
        model.set(o);
    }

//    public ResponseModel put(String key, Object value){
//        ResponseModel responseModel = this.getModel();
//        Object data = responseModel.getData();
//        if(data == null || !(data instanceof Map)){
//            data = new HashMap<String,Object>();
//            responseModel.setData(data);
//        }
//        Map<String,Object> map = (Map<String,Object>) data;
//        map.put(key,value);
//        return responseModel;
//    }

    public CallResult setData(Object data){
        CallResult res = this.getModel();
        res.setData(data);
        return res;
    }

    public CallResult setMsg(String msg){
        CallResult res = this.getModel();
        res.setMsg(msg);

        return res;
    }
}
